package cn.edu.scau.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.scau.dao.CommentDao;
import cn.edu.scau.entity.Blog;
import cn.edu.scau.entity.Comment;
import cn.edu.scau.entity.Reply;
import cn.edu.scau.service.CommentService;


@Service("commentServiceImpl")
public class CommentServiceImpl extends BaseServiceImpl<Comment> implements CommentService {

	@Autowired
	private CommentDao commentDao;

	public CommentDao getCommentDao() {
		return commentDao;
	}

	public void setCommentDao(CommentDao commentDao) {
		this.commentDao = commentDao;
	}

	/**
	 * 返回当前评论
	 * 
	 * @param commentId
	 * 评论id
	 */
	public Comment find(Integer commentId) {

		return this.getCommentDao().find(Comment.class, commentId);
	}

	/**
	 * 更新实体
	 * 
	 * @param entity
	 *  实体
	 * @param id
	 *实体id
	 */
	@Override
	public synchronized boolean update(Comment entity) {
		if (this.find(entity.getCommentId()) != null) {
			this.getCommentDao().save(entity);
			return true;
		}
		return false;
	}

	/**
	 * 回复
	 * 
	 * @param reply
	 *回复
	 * @param commentId
	 *评论id
	 * @return
	 */
	public boolean reply(Reply reply, Integer commentId) {

		Comment comment = this.find(commentId);
		comment.getReplies().add(reply);
		this.getCommentDao().update(comment);
		return true;
	}
}